Timestamp: Tue Mar 24 23:12:34 2020
Version: 1.1
License: CC-BY
dplyr provides a set of tools for efficiently manipulating datasets in R. It belongs to the tidyverse, which is a a collection of R packages designed for data science. All packages share an underlying design philosophy, grammar, and data structures.
What I personally love is that dplyr is synthetic, fast and easy to read. It allows to write code which is easy to explain, revise and mantain. Knowing that whather analysis we do, we will probably have to adjust and re-run it over and over, using dplyr’s grammar now is the best present we can give to our future selves, and will help us save a lot of time later.
The overarching goals of this tutorial is to fall in love with dplyr and to see some cute wild cats. The specific goals are:
- Understand the logic of dplyr and the power of piping %>%
- Explore the key verbs of dplyr’s grammar
- Simulate a dplyr based workflow
You will learn how to use the key verbs of dplyr, including select(), filter(), mutate(), summarize(), rename(), as well as their generalized x_at() and x_all() versions. We’ll also touch on the join family of functions.
We will first create a simulated dataframe using data from GBIF and environmental predictors from some global datasets.
We start loading some packages
library(tidyverse)
library(downloader)
library(rgbif)
## Spatial packages
#install.packages(c("rgdal", "sp", "sf", "raster", "rnaturalearth"))
library(rgdal)
library(sp)
library(sf)
library(raster)
library(rnaturalearth)
#save temporary files
rasterOptions(tmpdir="_tmp")
GBIFAs a toy dataset, we will compare the climatic niche of the five cutest wild cats out there. The selection follows the authoritative source www.backyardcatenclosures.com.au.
Let’s get familiar with our cute cats. We first create a data.frame of names, short names and urls to retrieve pictures of our cute cats:
myspecies <- c("Felis manul", #Palla's cat
"Caracal caracal", #Caracal
"Felis margarita", #Sand cat
"Prionailurus rubiginosus", #Rusty spotted cat
"Leopardus wiedii" #Margay
)
shortnames <- c("Palla's cat", "Caracal", "Sand Cat", "Rusty Spotted Cat", "Margay")
urls <- c("https://upload.wikimedia.org/wikipedia/commons/d/d6/Manoel.jpg", #pallas
"https://upload.wikimedia.org/wikipedia/commons/a/a3/Caracl_%2801%29%2C_Paris%2C_d%C3%A9cembre_2013.jpg",
#caracal
"https://upload.wikimedia.org/wikipedia/commons/e/e9/Persian_sand_CAT.jpg",
#Sand cat
"https://upload.wikimedia.org/wikipedia/commons/3/3e/Rusty_spotted_cat_1.jpg",
#Rusty spotted cat
"https://upload.wikimedia.org/wikipedia/commons/b/bd/Margay_in_Costa_Rica.jpg"
#Margay
)
cute <- data.frame(species=myspecies,
short=shortnames,
url=urls)